package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.ReportLocalDateDTO;
import com.sky.entity.Orders;
import com.sky.entity.User;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * 订单额统计，查询已完成订单的总金额
     * @param dto
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistic(ReportLocalDateDTO dto) {
        //获取所有区间内的订单
//        List<Orders> ordersList = orderMapper.queryByDateBetween(dto.getBegin(),dto.getEnd());
        //分日期获取订单金额
        List<LocalDate> localDates = new ArrayList<>();
        List<BigDecimal> turnovers = new ArrayList<>();
        for(LocalDate i = dto.getBegin() ; !i.isAfter(dto.getEnd()) ; i = i.plusDays(1)){
            localDates.add(i);

            LocalDateTime begin = LocalDateTime.of(i,LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(i,LocalTime.MAX);
            BigDecimal temp = orderMapper.countTurnoverInDay(begin , end);
            turnovers.add((temp == null) ? BigDecimal.valueOf(0) : temp);
        }
//        for (Orders orders : ordersList) {
//            if(orders.getStatus() == Orders.COMPLETED){
//                LocalDate localDate = orders.getDeliveryTime().toLocalDate();
//                int distance = localDate.compareTo(dto.getBegin());
//                turnovers.set(distance,turnovers.get(distance).add(orders.getAmount()));
//            }
//        }

        return TurnoverReportVO.builder()
                .turnoverList(StringUtils.join(turnovers , ","))
                .dateList(StringUtils.join(localDates , ","))
                .build();
    }

    /**
     * 统计新增用户和总用户数量
     * @param dto
     * @return
     */
    @Override
    public UserReportVO userStatistics(ReportLocalDateDTO dto) {
        //准备数据结构
        List<LocalDate> dateList = new ArrayList<>();
        List<Integer> totalUserList = new ArrayList<>();
        List<Integer> newUserList = new ArrayList<>();
        for(LocalDate i = dto.getBegin(); !i.isAfter(dto.getEnd()) ; i = i.plusDays(1)){
            dateList.add(i);
        }
        //读取数据库
        for(LocalDate i = dto.getBegin(); !i.isAfter(dto.getEnd()) ; i = i.plusDays(1)){
            LocalDateTime beginTime = LocalDateTime.of(i, LocalTime.MIN);
            beginTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            LocalDateTime endTime = LocalDateTime.of(i, LocalTime.MAX);
            
            Integer totalUserTemp = userMapper.countByLocalDate(null,endTime);
            if(Objects.isNull(totalUserTemp))
                totalUserList.add(0);
            else
                totalUserList.add(totalUserTemp);
            Integer newUserTemp = userMapper.countByLocalDate(beginTime,endTime);
            if(Objects.isNull(newUserTemp))
                newUserList.add(0);
            else
                newUserList.add(newUserTemp);

        }

        return UserReportVO.builder()
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .dateList(StringUtils.join(dateList, ","))
                .build();
    }
}
